const iconv = require('iconv-lite');


function parseContral(buffer) {
    // 4个空字节
    let way = iconv.decode(buffer.slice(4, 24), 'gbk'); //4~24
    let line = iconv.decode(buffer.slice(24, 44), 'gbk'); //24~44
    let mileageDir = !!buffer.readIntLE(44, 1); //44~45
    let carDir = !!buffer.readIntLE(45, 1); //45~46
    // 46~48补位
    let distance = buffer.readDoubleLE(48); //48~56
    let railType = buffer.readIntLE(56, 4); //56~60
    let status = !!buffer.readIntLE(60, 1); //60~61
    let saveImage = !!buffer.readIntLE(61, 1); //61~62
    let isKiloMeter = !!buffer.readIntLE(62, 1); //62~63
    let isHectoMeter = !!buffer.readIntLE(63, 1); //63~64
    let shutDown = !!buffer.readIntLE(64, 1); //64~65
    // 65~68补位
    let reserve = []; //68~108
    for (let i = 0; i < 10; i++) {
        let r = buffer.readIntLE(68 + i * 4, 4);
        reserve.push(r);
    }

    return {
        way: clearChar(way),
        line: clearChar(line),
        mileageDir,
        carDir,
        distance,
        railType,
        status,
        saveImage,
        isKiloMeter,
        isHectoMeter,
        shutDown,
        reserve
    }
}

function genContral(config) {
    let buffer = Buffer.alloc(108);

    let bway = iconv.encode(config.way, 'gbk');
    bway.copy(buffer, 4)

    let bline = iconv.encode(config.line, 'gbk')
    bline.copy(buffer, 24);

    buffer.writeIntLE(config.mileageDir, 44, 1);
    buffer.writeIntLE(config.carDir, 45, 1);

    buffer.writeDoubleLE(config.distance, 48);
    buffer.writeIntLE(config.railType, 56, 4);
    buffer.writeIntLE(config.status, 60, 1);
    buffer.writeIntLE(config.saveImage, 61, 1);
    buffer.writeIntLE(config.isKiloMeter, 62, 1);
    buffer.writeIntLE(config.isHectoMeter, 63, 1);
    buffer.writeIntLE(config.shutDown, 64, 1);

    for (let i = 0; i < config.reserve.length; i++) {
        buffer.writeIntLE(config.reserve[i], 68 + i * 4, 4);
    }

    return buffer;
}

function parseDot(buffer) {
    let isRight = !!buffer.readIntLE(0, 1); //0~1
    let curDistance = buffer.readDoubleLE(4); //4~12
    let dotCount = buffer.readIntLE(12, 4); //12~16

    let dots = [];
    try {
        for (let i = 0; i < dotCount; i++) {
            let x = buffer.readFloatLE(16 + i * 8);
            let y = buffer.readFloatLE(20 + i * 8);
            dots.push({ x, y });
        }
    } catch (err) {
        console.warn('Parse Error in i=', dots.length, 'stack', err.stack)
    }

    return {
        isRight,
        curDistance,
        dotCount,
        dots
    }

}

function genDot(dot) {
    let buffer = Buffer.alloc(10264);

    buffer.writeIntLE(dot.isRight, 0, 1); //0~1
    //1~4补位
    buffer.writeDoubleLE(dot.curDistance, 4); //4~12
    buffer.writeIntLE(dot.dotCount, 12, 4); //12~16

    for (let i = 0; i < dot.dots.length; i++) {
        buffer.writeFloatLE(dot.dots[i].x, 16 + i * 8);
        buffer.writeFloatLE(dot.dots[i].y, 20 + i * 8);
    }

    return buffer;
}

function clearChar(str) {
    return str.replace(/\0/g, '');
}

module.exports = {
    contral: {
        parse: parseContral,
        gen: genContral
    },
    dot: {
        parse: parseDot,
        gen: genDot
    }
};